home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / osi / isode / dosisode / DOSISODE80.ZIP / ISODE8.WRK / UNIX / LIB / INET.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-05  |  3.1 KB  |  141 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <netinet/in.h>
  4. #include <ctype.h>
  5.  
  6. unsigned long inet_addr(cp)
  7. char *cp;
  8. {
  9.   int i,j,ti[4];
  10.   unsigned long k;
  11.   char *q;
  12.   char s[10];
  13.  
  14.   q = cp;
  15.   j = 0;
  16.   while (!isspace(*q) && *q != '\0') {
  17.     if (*q == '.') q++;
  18.     i = 0;
  19.     while ((*q >= '0') && (*q <= '9')) {
  20.       s[i++] = *q;
  21.       q++;
  22.     }
  23.     if ((*q != '.') && (*q != '\0') && !isspace(*q)) return(0xffffffff);
  24.     s[i] = '\0';
  25.     if (j > 3) return(0xffffffff);
  26.     ti[j++] = atoi(s);
  27.   }
  28.   switch (j) {
  29.   case 1:
  30.     k = ti[0];
  31.     break;
  32.   case 2:
  33.     k = (ti[0] * 0x01000000) + ti[1];
  34.     break;
  35.   case 3:
  36.     k = (ti[0] * 0x01000000) + (ti[1] * 0x00010000) + ti[2];
  37.     break;
  38.   case 4:
  39.     k = (ti[0] * 0x01000000) + (ti[1] * 0x00010000) + (ti[2] * 0x00000100) + ti[3];
  40.     break;
  41.   }
  42.   k = htonl(k);
  43.   return(k);
  44. }
  45.  
  46. char *inet_ntoa(in)
  47. struct in_addr in;
  48. {
  49.    char *s;
  50.    unsigned char *p;
  51.  
  52.    p = (unsigned char *)∈
  53.    s = (char *)malloc(16);
  54. /*   sprintf(s,"%d.%d.%d.%d",in.s_net,in.s_host,in.s_lh,in.s_impno);*/
  55.    sprintf(s,"%d.%d.%d.%d",p[0],p[1],p[2],p[3]);
  56.    return(s);
  57. }
  58. inet_network(name)
  59.   char *name;
  60. {
  61.   long bytes[4];
  62.   int byte_ctr = -1;
  63.   int i;
  64.   int state = 0;  /* init bytes jobber */
  65.   int base;
  66.   unsigned long value;
  67.  
  68.   while (*name != '\0') {
  69.     if (state == 0) { /* at beginning of byte */
  70.       byte_ctr++;
  71.       if (byte_ctr > 4)
  72.          return (-1);
  73.       bytes[byte_ctr] = 0;
  74.       if (*name == '0') {
  75.          base = 8;
  76.      name++;
  77.       }
  78.       if ((*name == 'x') || (*name == 'X')) {
  79.          base = 16;
  80.      name++;
  81.       }
  82.       state = 1;
  83.     }
  84.     else {  /* state != 0 */
  85.       if (*name == '.') {
  86.          name++;
  87.          state = 0;
  88.          continue;
  89.       }
  90.       if (isdigit(*name)) {
  91.      bytes[byte_ctr] = (bytes[byte_ctr] * base) + (*name - '0');
  92.      name++;
  93.      continue;
  94.       }
  95.       if ((base == 16) && isxdigit(*name)) { /* a-f,A-F */
  96.          *name = toupper(*name);
  97.          bytes[byte_ctr] = (bytes[byte_ctr] * base) + (10 + (*name - 'A'));
  98.      name++;
  99.      continue;
  100.       }
  101.       if (*name && (!isspace(*name)))
  102.          return(-1);
  103.     } /* end of state != 0 */
  104.   } /* end of while loop */
  105.  
  106.   for(value = 0,i=0; i < byte_ctr; i++) {
  107.      value = value << 8;
  108.      value = value | (bytes[i] & 0xFF);
  109.   }
  110.  
  111.   return(value);
  112.  
  113. } /* end of inet_network */
  114.  
  115. inet_lnaof(in)
  116.         struct in_addr in;
  117. {
  118.         unsigned long addr = ntohl(in.s_addr);
  119.  
  120.         if ((addr & 0x80000000) == 0 ) /* class A */
  121.            return(addr & 0x00FFFFFF);
  122.         else if ((addr & 0xC0000000) == 0x80000000) /* class B */
  123.            return(addr & 0x0000FFFF);
  124.         else  /* class C */
  125.            return(addr & 0x000000FF);
  126. }
  127.  
  128. inet_netof(in)
  129.     struct in_addr in;
  130. {
  131.         unsigned long addr = ntohl(in.s_addr);
  132.  
  133.         if ((addr & 0x80000000) == 0 ) /* class A */
  134.            return(addr & 0xFF000000);
  135.         else if ((addr & 0xC0000000) == 0x80000000) /* class B */
  136.            return(addr & 0xFFFF0000);
  137.         else  /* class C */
  138.            return(addr & 0xFFFFFF00);
  139. }
  140.  
  141.